package com.lc.cehui.ui.activity.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.dialog.AddressDialog;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.AddressAddApi;
import com.lc.cehui.http.response.RegisterBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.util.CourseUtils;
import com.lc.cehui.util.SPUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class InsertAddrActivity extends AppActivity {
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_number)
    EditText etNumber;
    @BindView(R.id.tv_address_details)
    EditText tvAddressDetails;
    @BindView(R.id.tv_door_num)
    EditText tvDoorNum;
    @BindView(R.id.switch_accelerate)
    Switch switchAccelerate;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    private String id="";
    /**
     * 省
     */
    private String mProvince = "";
    /**
     * 市
     */
    private String mCity = "";
    /**
     * 区
     */
    private String mArea = "";

    @Override
    protected int getLayoutId() {
        return R.layout.insert_addr_activity;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        if (CourseUtils.isNotNullOrEmpty(getIntent().getStringExtra("type"))) {
            etName.setText(getIntent().getStringExtra("name"));
            etNumber.setText(getIntent().getStringExtra("mobile"));
            tvDoorNum.setText(getIntent().getStringExtra("room"));
            tvAddressDetails.setText(getIntent().getStringExtra("address"));

            etName.setText(getIntent().getStringExtra("name"));
            id = getIntent().getStringExtra("id");
            mProvince = getIntent().getStringExtra("province");
           mCity = getIntent().getStringExtra("city");
            mArea = getIntent().getStringExtra("district");
            tvAddress.setText( mProvince + mCity + mArea);
            if( getIntent().getStringExtra("is_check").equals("2")){
                switchAccelerate.setChecked(true);
            }else {
                switchAccelerate.setChecked(false);
            }
        }
    }


    @OnClick({R.id.tv_address, R.id.bt_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_address:
                new AddressDialog.Builder(this)
                        .setTitle("选择地区")
                        // 设置默认省份
//                        .setProvince(mProvince)
                        // 设置默认城市（必须要先设置默认省份）
//                        .setCity(mCity)
                        // 不选择县级区域
                        //.setIgnoreArea()
                        .setListener((dialog, province, city, area) -> {

                            String address = province + city + area;
                            if (!tvAddress.getText().toString().equals(address)) {
                                mProvince = province;
                                mCity = city;
                                mArea = area;
                                tvAddress.setText(address);
                            }
                        })
                        .show();
                break;
            case R.id.bt_submit:
                address_add();
                break;
        }
    }

    private void address_add() {
        EasyHttp.post(this)
                .api(new AddressAddApi()
                        .setMobile(etNumber.getText().toString()).setNames(etName.getText().toString()).setProvince(mProvince)
                        .setCity(mCity)
                        .setDistrict(mArea).setUserid(SPUtil.get(this, IntentKey.USERID, 0)).setAddress(tvAddressDetails.getText().toString()).setRoom(tvDoorNum.getText().toString()).setIs_check(switchAccelerate.isChecked() ? "2" : "1").setArticle_id(id))
                .request(new HttpCallback<RegisterBean>(this) {

                    @Override
                    public void onSucceed(RegisterBean data) {
                        toast(data.getMsg());
                        if (data.getCode().equals("200")) {
                            finish();
                        }

                    }
                });
    }
}
